package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.*;
import com.eastfair.venueservice.entity.WorkOrder;
import com.eastfair.venueservice.service.WorkOrderService;
import com.eastfair.venueservice.vo.WorkOrderVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 工单
 * </p>
 *
 * @author linan
 * @date 2022-10-14
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/workOrder")
@Api(value = "WorkOrder", tags = "工单")
@PreAuth(replace = "venueservice:workOrder:")
public class WorkOrderController extends SuperController<WorkOrderService, Long, WorkOrder, WorkOrderPageQuery, WorkOrderSaveDTO, WorkOrderUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<WorkOrder> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<WorkOrder> workOrderList = list.stream().map((map) -> {
            WorkOrder workOrder = WorkOrder.builder().build();
            //TODO 请在这里完成转换
            return workOrder;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(workOrderList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<WorkOrderPageQuery> params) {
        WorkOrderPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 生成派工单.
     *
     * @param workOrderDTO workOrderDTO
     * @return the r
     */
    @SysLog("生成派工单")
    @PostMapping("/generateWorkOrder")
    public R<Integer> generateWorkOrder(@RequestBody WorkOrderDTO workOrderDTO){
        return R.success(baseService.generateWorkOrder(workOrderDTO));
    }

    /**
     * 批量生成派工单.
     *
     * @param workOrderDTO workOrderDTO
     * @return the r
     */
    @SysLog("生成派工单")
    @PostMapping("/generateWorkOrderBatch")
    public R<Integer> generateWorkOrderBatch(@RequestBody List<WorkOrderDTO> workOrderDTO){
        return R.success(baseService.generateWorkOrderBatch(workOrderDTO));
    }

    /**
     * 修改派工单.
     *
     * @param workOrderDTO workOrderDTO
     * @return the r
     */
    @SysLog("修改派工单")
    @PostMapping("/updateWorkOrder")
    public R<Boolean> updateWorkOrder(@RequestBody WorkOrderDTO workOrderDTO){
        return R.success(baseService.updateWorkOrder(workOrderDTO));
    }

    /**
     * 查询派工单.
     *
     * @param workOrderQuery workOrderQuery
     * @return the r
     */
    @PostMapping("/queryWorkOrder")
    public R<Page<WorkOrderVO>> queryWorkOrder(@RequestBody PageParams<WorkOrderQuery> workOrderQuery){
        return R.success(baseService.queryWorkOrder(workOrderQuery));
    }

    /**
     * 查询派工单明细.
     *
     * @param workOrderQuery workOrderQuery
     * @return the r
     */
    @PostMapping("/queryWorkOrderDetail")
    public R<WorkOrderVO> queryWorkOrderDetail(@RequestBody WorkOrderQuery workOrderQuery){
        return R.success(baseService.queryWorkOrderDetail(workOrderQuery));
    }

    /**
     * 接收工单.
     *
     * @param workOrderOperationRecordDTO workOrderOperationRecordDTO
     * @return the r
     */
    @SysLog("接收工单")
    @PostMapping("/receiveWorkOrder")
    public R<Boolean> receiveWorkOrder(@RequestBody WorkOrderOperationRecordDTO workOrderOperationRecordDTO){
        return R.success(baseService.receiveWorkOrder(workOrderOperationRecordDTO));
    }

    /**
     * 重新派工.
     *
     * @param workOrderOperationRecordDTO workOrderOperationRecordDTO
     * @return the r
     */
    @SysLog("重新派工")
    @PostMapping("/reassign")
    public R<Boolean> reassign(@RequestBody WorkOrderOperationRecordDTO workOrderOperationRecordDTO){
        return R.success(baseService.reassign(workOrderOperationRecordDTO));
    }

    /**
     * 撤销工单.
     *
     * @param workOrderOperationRecordDTO workOrderOperationRecordDTO
     * @return the r
     */
    @SysLog("撤销工单")
    @PostMapping("/revokeWorkOrder")
    public R<Boolean> revokeWorkOrder(@RequestBody WorkOrderOperationRecordDTO workOrderOperationRecordDTO){
        return R.success(baseService.revokeWorkOrder(workOrderOperationRecordDTO));
    }

    /**
     * 发起人撤销工单.
     *
     * @param workOrderOperationRecordDTO workOrderOperationRecordDTO
     * @return the r
     */
    @SysLog("撤销工单")
    @PostMapping("/revokeWorkOrderByPromoter")
    public R<Boolean> revokeWorkOrderByPromoter(@RequestBody WorkOrderOperationRecordDTO workOrderOperationRecordDTO){
        return R.success(baseService.revokeWorkOrderByPromoter(workOrderOperationRecordDTO));
    }

    /**
     * 作废工单.
     *
     * @param workOrderOperationRecordDTO workOrderOperationRecordDTO
     * @return the r
     */
    @SysLog("作废工单")
    @PostMapping("/cancelWorkOrder")
    public R<Boolean> cancelWorkOrder(@RequestBody WorkOrderOperationRecordDTO workOrderOperationRecordDTO){
        return R.success(baseService.cancelWorkOrder(workOrderOperationRecordDTO));
    }

    /**
     * 完结工单.
     *
     * @param workOrderOperationRecordDTO workOrderOperationRecordDTO
     * @return the r
     */
    @SysLog("完结工单")
    @PostMapping("/closeWorkOrder")
    public R<Boolean> closeWorkOrder(@RequestBody WorkOrderOperationRecordDTO workOrderOperationRecordDTO){
        return R.success(baseService.closeWorkOrder(workOrderOperationRecordDTO));
    }


    /**
     * 退回工单.
     *
     * @param workOrderOperationRecordDTO workOrderOperationRecordDTO
     * @return the r
     */
    @SysLog("退回工单")
    @PostMapping("/returnWorkOrder")
    public R<Boolean> returnWorkOrder(@RequestBody WorkOrderOperationRecordDTO workOrderOperationRecordDTO){
        return R.success(baseService.returnWorkOrder(workOrderOperationRecordDTO));
    }


    /**
     * 短信提醒.
     *
     * @param workOrderDTO workOrderDTO
     * @return the r
     */
    @SysLog("短信提醒")
    @PostMapping("/smsReminder")
    public R<Boolean> smsReminder(@RequestBody WorkOrderDTO workOrderDTO){
        return R.success(baseService.smsReminder(workOrderDTO));
    }

}
